package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.Admin;
import com.wanmait.employment.entity.CompanyType;
import com.wanmait.employment.mapper.AdminMapper;
import com.wanmait.employment.mapper.CompanyTypeMapper;
import com.wanmait.employment.service.CompanyTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CompanyTypeServiceImpl implements CompanyTypeService {
    @Resource
    private CompanyTypeMapper companyTypeMapper;

    @Resource
    private AdminMapper adminMapper;
    @Override
    public PageInfo<CompanyType> selectList(String title, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<CompanyType> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("title",title);
        List<CompanyType> companyTypeList=companyTypeMapper.selectList(queryWrapper);
        PageInfo<CompanyType> companyTypePageInfo=new PageInfo<>(companyTypeList);
        companyTypePageInfo.getList().forEach(companyType ->{
             QueryWrapper<Admin> adminQueryWrapper=new QueryWrapper<>();
             adminQueryWrapper.eq("id",companyType.getAdminId());
             Admin admin=adminMapper.selectOne(adminQueryWrapper);
             companyType.setAdmin(admin);
        });

        return companyTypePageInfo;
    }

    @Override
    public Boolean insert(CompanyType companyType) {
        LambdaQueryWrapper<CompanyType> companyTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        companyTypeLambdaQueryWrapper.eq(CompanyType::getTitle,companyType.getTitle());
        CompanyType companyType1=companyTypeMapper.selectOne(companyTypeLambdaQueryWrapper);
        if (companyType1==null){
        companyType.setAdminId(1);
        companyTypeMapper.insert(companyType);
        return true;
        }
        else {
            return false;
        }
    }

    @Override
    public void update(CompanyType companyType) {
      companyTypeMapper.updateById(companyType);
    }

    @Override
    public void delete(Integer id) {
       companyTypeMapper.deleteById(id);
    }

    @Override
    public List<CompanyType> findAll() {
        List<CompanyType> companyTypeList=companyTypeMapper.selectList(null);
        return companyTypeList;
    }
}
